package org.jrfe.gfx.g2d.shapes;

import java.nio.FloatBuffer;

import org.jrfe.gfx.BlendFunc;
import org.jrfe.gfx.ColorRGBA;
import org.jrfe.gfx.VertexBatch;
import org.jrfe.math.Vector2f;
import org.jrfe.res.Texture;

public abstract class AbstractShape implements Shape {

	protected VertexBatch vertexBatch;
	protected BlendFunc blendFunc;
	protected MappedTexture mappedTexture;
	protected ColorRGBA color;

	protected Vector2f location;
	protected Vector2f scale;
	protected float rotation;
	protected int elements;

	public AbstractShape(int elements, int primitiveType) {
		this(elements, null, 0, null, null, null, primitiveType);
	}

	public AbstractShape(int elements, Vector2f location, float rotation,
			Vector2f scale, int primitiveType) {
		this(elements, location, rotation, scale, null, null,
				primitiveType);
	}

	public AbstractShape(int elements, Vector2f location, float rotation,
			Vector2f scale, BlendFunc blendFunc,
			ColorRGBA color, int primitiveType) {

		this.elements = elements;
		this.location = location;
		this.rotation = rotation;
		this.scale = scale;
		this.blendFunc = blendFunc;
		this.color = color;

		if (location == null) {
			this.location = new Vector2f();
		}

		if (scale == null) {
			this.scale = new Vector2f();
		}

		vertexBatch = new VertexBatch(elements);
		vertexBatch.setPtype(primitiveType);
		vertexBatch.setTexture(0);
		
		if (color == null) {
			this.color = ColorRGBA.WHITE;
		}
		setColor(this.color);

		if (blendFunc == null) {
			this.blendFunc = BlendFunc.SIMPLE;
		}

	}

	public void setMappedTexture(MappedTexture mapt){
		this.mappedTexture = mapt;
		updateTexture();
		updateTextureCords();
	}
	
	public void updateTextureCords(){
		
	}
	
	public void updateTexture(){
		if(mappedTexture != null){
			getBatch().setTexture(mappedTexture.getTexture().getTextureID());
		}else{
			getBatch().setTexture(0);
		}
	}
	
	public MappedTexture getMappedTexture() {
		return mappedTexture;
	}

	public void setColor(ColorRGBA color) {
		this.color = color;
		for (int i = 0; i < vertexBatch.getMaxElements(); i++) {
			vertexBatch.updateColor(i, color);
		}
	}

	
	
	
	@Override
	public Vector2f getScale() {
		// TODO Auto-generated method stub
		return scale;
	}

	@Override
	public Vector2f getLocation() {
		// TODO Auto-generated method stub
		return location;
	}

	@Override
	public float getRotation() {
		// TODO Auto-generated method stub
		return rotation;
	}
	
	public void setScale(Vector2f scale) {
		this.scale = scale;
	}
	
	public void setRotation(float rotation) {
		this.rotation = rotation;
	}
	
	public void setLocation(Vector2f location) {
		this.location = location;
	}

	public void rotate(float a) {
		//TODO rotates shape
	}

	public void scale(float x, float y) {

		FloatBuffer buf = vertexBatch.getVertexs();
		int idx;
		for (int i = 0; i < vertexBatch.getElements(); i++) {
			idx = i * 2;
			buf.put(idx, buf.get(idx) * x);
			buf.put(idx + 1, buf.get(idx + 1) * y);
		}

	}

	public void translate(float x, float y) {

		FloatBuffer buf = vertexBatch.getVertexs();
		int idx;
		for (int i = 0; i < vertexBatch.getElements(); i++) {
			idx = i * 2;
			buf.put(idx, buf.get(idx) + x);
			buf.put(idx + 1, buf.get(idx + 1) + y);
		}

	}

	public ColorRGBA getColor() {
		return color;
	}


	
	@Override
	public VertexBatch getBatch() {
		return vertexBatch;
	}

	@Override
	public BlendFunc getBlending() {
		return blendFunc;
	}

	public void setBlendFunc(BlendFunc blendFunc) {
		this.blendFunc = blendFunc;
	}
}
